package com.gzz.orchid.main.console.controller.rbac;


import com.gzz.orchid.common.boot.asynclock.DistributedLockHepler;
import com.gzz.orchid.common.boot.mybatis.dbraw.MyBatisRawHelper;
import com.gzz.orchid.common.core.model.ActionCmd;
import com.gzz.orchid.common.core.validation.ValidGroup;
import com.gzz.orchid.common.webmvc.support.HttpResult;
import com.gzz.orchid.context.application.rbac.IUserExecutor;
import com.gzz.orchid.context.application.rbac.IUserHandler;
import com.gzz.orchid.context.application.rbac.command.ChangePasswordCmd;
import com.gzz.orchid.context.application.rbac.command.UserGrantCmd;
import com.gzz.orchid.context.application.rbac.command.UserSaveCmd;
import com.gzz.orchid.context.application.rbac.pojo.UserDto;
import com.gzz.orchid.context.application.rbac.pojo.UserVo;
import com.gzz.orchid.context.application.rbac.queries.UserQuery;
import com.gzz.orchid.context.domain.rbac.entity.UserId;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author higmd
 */
@Slf4j
@RestController
@RequestMapping("/rbac/user")
@RequiredArgsConstructor
public class UserController {
    private final IUserExecutor executor;
    private final IUserHandler handler;
    private final MyBatisRawHelper myBatisRawHelper;
    private final DistributedLockHepler distributedLockHepler;

    /**
     * 授权 用户特权
     * TODO: 待完善
     */
    @RequestMapping("/grant-privilege")
    public HttpResult grantPrivilege(UserGrantCmd cmd) {
        log.info("授权: {}", cmd);
        executor.grantPrivilege(cmd);
        return HttpResult.success();
    }

    /**
     * 修改密码
     */
    @RequestMapping("/change-password")
    public HttpResult changePassword(ChangePasswordCmd cmd) {
        log.info("修改密码:{}", cmd);
        executor.changePassword(cmd);
        return HttpResult.success();
    }

    /**
     * 重置密码
     */
    @RequestMapping("/reset-password")
    public HttpResult resetPassword(String userId) {
        log.info("重置密码userId: {}", userId);
        executor.resetPassword(new UserId(userId));
        return HttpResult.success();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public HttpResult list(UserQuery query) {
        log.info("列表:{}", query);
        List<UserDto> pageData = handler.listByPager(query);
        return HttpResult.success()
                .data(pageData)
                .pager(query.getPager());
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public HttpResult detail(@NotNull(message = "required.parameter") Long id) {
        log.info("详情ID: {}", id);
        UserVo detail = handler.detail(id);
        return HttpResult.success()
                .data(detail);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public HttpResult save(@Valid UserSaveCmd saveCmd) {
        log.info("保存:{}", saveCmd);
        executor.save(saveCmd);
        return HttpResult.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    public HttpResult edit(@Validated({ValidGroup.Update.class}) UserSaveCmd saveCmd) {
        log.info("修改:{}", saveCmd);
        executor.save(saveCmd);
        return HttpResult.success();
    }

    /**
     * 创建
     */
    @PostMapping("/create")
    public HttpResult create(@Validated({ValidGroup.Insert.class}) UserSaveCmd saveCmd) {
        log.info("创建:{}", saveCmd);
        executor.save(saveCmd);
        return HttpResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public HttpResult delete(@Valid ActionCmd cmd) {
        log.info("删除:{}", cmd);
        executor.delete(cmd);
        return HttpResult.success();
    }

    /**
     * 禁用
     */
    @RequestMapping("/disable")
    public HttpResult disable(@Valid ActionCmd cmd) {
        log.info("禁用:{}", cmd);
        executor.disable(cmd);
        return HttpResult.success();
    }

    /**
     * 启用
     */
    @RequestMapping("/enable")
    public HttpResult enable(@Valid ActionCmd cmd) {
        log.info("启用:{}", cmd);
        executor.enable(cmd);
        return HttpResult.success();
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    public HttpResult audit(@Valid ActionCmd cmd) {
        log.info("审核:{}", cmd);
        executor.audit(cmd);
        return HttpResult.success();
    }
}
